package com.panshi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.panshi.IZxArticleApi;
import com.panshi.IZxCommentApi;
import com.panshi.api.UserApi;
import com.panshi.domain.TypeVo;
import com.panshi.domain.ZxArticle;
import com.panshi.domain.ZxComment;
import com.panshi.domain.vo.PageBean;
import com.panshi.domain.vo.ZxResult;
import com.panshi.utils.LoginVerifyShowPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
public class ArticleController {

    @Autowired
    IZxArticleApi iZxArticleApi;

    @Autowired
    UserApi userApi;

    @Autowired
    IZxCommentApi iZxCommentApi;

    //咨询（今日推荐）
    @RequestMapping("/consult")
    public ModelAndView consult(HttpSession httpSession){

        ModelAndView modelAndView = new ModelAndView();

        //类型
        ZxResult queryTypeList = iZxArticleApi.queryType();
        modelAndView.addObject("queryTypeList",queryTypeList.getDate());

        //咨询首页数据（随机）
        modelAndView.addObject("articleList",iZxArticleApi.articleList());

        //大家都在看栏
        ZxResult everyoneLookList = iZxArticleApi.everyoneLook();
        modelAndView.addObject("everyoneLookList",everyoneLookList.getDate());

        //专家推荐栏
        ZxResult expertRecommendList = iZxArticleApi.expertRecommend();
        modelAndView.addObject("expertRecommendList",expertRecommendList.getDate());

        //登录后展示头部栏
        modelAndView.addObject("userObject",new LoginVerifyShowPage().showPage(userApi,httpSession));

        modelAndView.setViewName("consult");
        return modelAndView;
    }

    //咨询（推荐，热点，最新）
    @RequestMapping("/index/item")
    public ModelAndView articleFunction(int current,int type){

        ModelAndView modelAndView = new ModelAndView();
        //当前页
        int currentPage=current*10-10;

        TypeVo typeVo=new TypeVo();
        typeVo.setCurrentPage(currentPage);
        typeVo.setType(type);

        //咨询
        modelAndView.addObject("articleList",iZxArticleApi.articleListCondition(typeVo));

        modelAndView.setViewName("articleDiv");
        return modelAndView;
    }

    //咨询 分页
    @RequestMapping("/articleFy")
    public ModelAndView articlePaging(int typeId,int current,int type,HttpSession httpSession){

        ModelAndView modelAndView = new ModelAndView();

        //类型
        ZxResult queryTypeList = iZxArticleApi.queryType();
        modelAndView.addObject("queryTypeList",queryTypeList.getDate());

        //类型id
        modelAndView.addObject("typeId",typeId);
        modelAndView.addObject("type",type);

        //看点
        modelAndView.addObject("articleRandomQuerySix",iZxArticleApi.articleRandomQuerySix());

        PageBean pageBean=new PageBean();
        pageBean.setTid(typeId);
        pageBean.setCurrentPage(current*10-10);
        pageBean.setPageSize(10);
        pageBean.setType(type);

        Map zxArticleBayType = iZxArticleApi.getZxArticleBayType(pageBean);
        modelAndView.addObject("totalArticleList",zxArticleBayType.get("data"));
        modelAndView.addObject("recommendList",zxArticleBayType.get("correlation"));

        int total = (Integer) zxArticleBayType.get("total");
        int num=(total / 10) + (total % 10 > 0 ? 1: 0);

        modelAndView.addObject("pageNum",num);
        modelAndView.addObject("currentPage",current);

        //登录后展示头部栏
        modelAndView.addObject("userObject",new LoginVerifyShowPage().showPage(userApi,httpSession));
        modelAndView.setViewName("type");
        return modelAndView;
    }

    //咨询 热点
    @RequestMapping("/hot/{typeId}")
    public ModelAndView articleHot(@PathVariable Integer typeId,HttpSession httpSession){

        ModelAndView modelAndView = new ModelAndView();

        //类型
        ZxResult queryTypeList = iZxArticleApi.queryType();
        modelAndView.addObject("queryTypeList",queryTypeList.getDate());

        //类型id
        modelAndView.addObject("typeId",typeId);
        modelAndView.addObject("type",10);

        PageBean pageBean=new PageBean();
        pageBean.setTid(typeId);
        pageBean.setCurrentPage(1);
        pageBean.setPageSize(10);
        pageBean.setType(10);

        Map zxArticleBayType = iZxArticleApi.getZxArticleBayType(pageBean);
        /*咨询类型详情*/
        modelAndView.addObject("totalArticleList",zxArticleBayType.get("data"));
        /*相关推荐*/
        modelAndView.addObject("recommendList",zxArticleBayType.get("correlation"));

        int total = (Integer) zxArticleBayType.get("total");
        int num=(total / 10) + (total % 10 > 0 ? 1: 0);
        modelAndView.addObject("pageNum",num);
        modelAndView.addObject("currentPage",1);

        //看点
        modelAndView.addObject("articleRandomQuerySix",iZxArticleApi.articleRandomQuerySix());

        //登录后展示头部栏
        modelAndView.addObject("userObject",new LoginVerifyShowPage().showPage(userApi,httpSession));

        modelAndView.setViewName("type");
        return modelAndView;
    }

    //咨询 最新
    @RequestMapping("/new/{typeId}")
    public ModelAndView articleNew(@PathVariable Integer typeId,HttpSession httpSession){

        ModelAndView modelAndView = new ModelAndView();

        //类型
        ZxResult queryTypeList = iZxArticleApi.queryType();
        modelAndView.addObject("queryTypeList",queryTypeList.getDate());

        //类型id
        modelAndView.addObject("typeId",typeId);
        modelAndView.addObject("type",20);

        PageBean pageBean=new PageBean();
        pageBean.setTid(typeId);
        pageBean.setCurrentPage(1);
        pageBean.setPageSize(10);
        pageBean.setType(20);

        Map zxArticleBayType = iZxArticleApi.getZxArticleBayType(pageBean);
        /*咨询类型详情*/
        modelAndView.addObject("totalArticleList",zxArticleBayType.get("data"));
        /*相关推荐*/
        modelAndView.addObject("recommendList",zxArticleBayType.get("correlation"));

        int total = (Integer) zxArticleBayType.get("total");
        int num=(total / 10) + (total % 10 > 0 ? 1: 0);
        modelAndView.addObject("pageNum",num);
        modelAndView.addObject("currentPage",1);

        //看点
        modelAndView.addObject("articleRandomQuerySix",iZxArticleApi.articleRandomQuerySix());

        //登录后展示头部栏
        modelAndView.addObject("userObject",new LoginVerifyShowPage().showPage(userApi,httpSession));

        modelAndView.setViewName("type");
        return modelAndView;
    }

    //文章详情（咨询）
    @RequestMapping("/articleDetail")
    public ModelAndView articleDetail(int articleId,HttpSession httpSession){
        ModelAndView modelAndView = new ModelAndView();

        PageBean pageBean=new PageBean();
        pageBean.setZxId(articleId);
        pageBean.setCurrentPage(0);

        Map map= iZxArticleApi.ZxBayId(pageBean);

        Object obj =map.get("zxArticle");

        ZxArticle zxArticle = JSON.parseObject(obj.toString(),ZxArticle.class);

        Object listObj =map.get("comments");
        List<ZxComment> zxCommentList = JSONArray.parseArray(listObj.toString(),ZxComment.class);

        //类型id
        modelAndView.addObject("typeId",zxArticle.getTypeId());

        //类型
        ZxResult queryTypeList = iZxArticleApi.queryType();
        modelAndView.addObject("queryTypeList",queryTypeList.getDate());

        /*文章详情*/
        modelAndView.addObject("articleDetail",zxArticle);

        /*当前时间*/
        modelAndView.addObject("currentTime",new Date().getTime());

        /*文章详情评论*/
        modelAndView.addObject("articleDetailComments",zxCommentList);



        //看点
        modelAndView.addObject("articleRandomQuerySix",iZxArticleApi.articleRandomQuerySix());

        /*相关推荐*/
        TypeVo typeVo=new TypeVo();
        typeVo.setCurrentPage(0);
        typeVo.setType(zxArticle.getTypeId());
        typeVo.setArticleId(articleId);
        modelAndView.addObject("articleList",iZxArticleApi.articleDetaiRecommendlList(typeVo));

        //登录后展示头部栏
        modelAndView.addObject("userObject",new LoginVerifyShowPage().showPage(userApi,httpSession));

        modelAndView.setViewName("articleDetail");
        return modelAndView;
    }

    //咨询详情  相关推荐
    @RequestMapping("/recommend")
    public ModelAndView recommend(int current,int typeId){
        ModelAndView modelAndView=new ModelAndView();

        /*相关推荐*/
        TypeVo typeVo=new TypeVo();
        typeVo.setCurrentPage(current*10-10);
        typeVo.setType(typeId);
        modelAndView.addObject("articleList",iZxArticleApi.articleDetaiRecommendlList(typeVo));

        modelAndView.setViewName("articleDiv");
        return modelAndView;
    }

    //咨询详情发表评论
    @RequestMapping("/addComment")
    @ResponseBody
    public ZxResult publishArticleComment(String content,int pid,int articleId,int type,HttpSession httpSession){
        Integer userId=(Integer)httpSession.getAttribute(httpSession.getId());

        ModelAndView modelAndView=new ModelAndView();

        ZxComment zxComment=new ZxComment();
        zxComment.setAticleId(articleId);
        zxComment.setContent(content);
        zxComment.setUserId(userId);

        if(pid == 0){
            zxComment.setLevel(0);
            zxComment.setParentId(0);
        }
        if(pid != 0){
            zxComment.setLevel(1);
            zxComment.setParentId(pid);
        }

        System.out.println(zxComment.toString());

        ZxResult zxResult = iZxCommentApi.addComment(zxComment);
        return zxResult;
    }

    @RequestMapping("/showPl")
    public ModelAndView showPl(int id){
        ModelAndView modelAndView=new ModelAndView();

        PageBean pageBean=new PageBean();
        pageBean.setZxId(id);
        pageBean.setCurrentPage(0);

        ZxResult commentReply = iZxCommentApi.getCommentReply(pageBean);
        modelAndView.addObject("showPl",commentReply.getDate());

        modelAndView.setViewName("showPl");
        return modelAndView;
    }

    @RequestMapping("/commentOption")
    @ResponseBody
    public ZxResult dianzan(int cid,HttpSession httpSession){

        System.out.println("cid--------------------------------------"+cid);

        Integer userId=(Integer)httpSession.getAttribute(httpSession.getId());
        ZxComment zxComment=new ZxComment();
        zxComment.setUserId(userId);
        zxComment.setId(cid);

        ZxResult zxResult = iZxCommentApi.ZxCommentRaise(zxComment);
        System.out.println("点赞："+zxResult);
        return zxResult;
    }
}
